package sprites;

import java.awt.image.BufferedImage;

import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.Timer;

import engine.TowerDefense;
import sprites.Projectile;

/**
 * An abstract tower.
 * 
 * @author Alex Lee (hl69)
 */
public abstract class Tower extends TDEntity
{
	/*
	 * Class constants.
	 */
	public static int INFINITE_RANGE = TowerDefense.COLS*TowerDefense.TILESIZE;
	
	/*
	 * Towers can attack.
	 */
	Timer firingRate;
	boolean firing;
	double range;

	public Tower(TowerDefense owner, BufferedImage[] image, double x, double y)
	{
		super(owner, image, x, y);
		
		firing = false;
	}
	
	public void update(long elapsedTime)
	{
		/*
		 * Only fire when an enemy exists within the range.
		 */
		firing = false;
		Sprite[] enemies = myOwner.ENEMIES.getSprites();
		for(int i = 0; i < enemies.length; i++)
		{
			if(enemies[i] != null && enemies[i].isActive() &&
					withinRange(enemies[i]))
			{
				firing = true;
			}
		}
	}

	/**
	 * Checks if the other entity is within the attack range of this one.
	 * @param other
	 * @return
	 */
	private boolean withinRange(Sprite other)
	{
		if(other.getX() <= getX()+range && other.getY() == getY()) return true;
		else return false;
	}

}
